package com.hualan.sfzy.controller;

import com.hualan.sfzy.model.dto.ArticleDto;
import com.hualan.sfzy.model.dto.ReadReportDto;
import com.hualan.sfzy.model.entity.Article;
import com.hualan.sfzy.model.vo.ArticleQuery;
import com.hualan.sfzy.service.ArticleService;
import com.hualan.sfzy.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

import static com.hualan.sfzy.constant.CommonConstant.*;

/**
 * 文章管理控制器
 */
@RestController
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    /**
     * 添加文章
     */
    @PostMapping("/add")
    public Result add(@RequestBody Article article) {
        int result = articleService.add(article);
        return result > 0 ? Result.success() : Result.error();
    }

    /**
     * 编辑文章
     */
    @PutMapping("/edit")
    public Result edit(@RequestBody Article article) {
        int result = articleService.edit(article);
        return result > 0 ? Result.success() : Result.error();
    }

    /**
     * 删除文章
     */
    @DeleteMapping("/remove/{id}")
    public Result remove(@PathVariable Integer id) {
        int result = articleService.remove(id);

        return result > 0 ? Result.success() : Result.error();
    }

    /**
     * 批量删除文章
     */
    @DeleteMapping("/removeBatch")
    public Result removeBatch(@RequestBody Integer[] ids) {
        int result = articleService.removeBatch(ids);
        return result > 0 ? Result.success() : Result.error();
    }

    /**
     * 查询所有文章
     */
    @GetMapping("/find")
    public Result find() {
        List<ArticleDto> articles = articleService.findAll();
        return Result.success(articles);
    }

    /**
     * 分页查询文章
     */
//    @GetMapping("/findPage")
//    public Result findPage(@RequestParam(value = PAGE_NUM, defaultValue = PAGE_NUM_DEFAULT) Integer pageNum,
//                          @RequestParam(value = PAGE_SIZE, defaultValue = PAGE_SIZE_DEFAULT) Integer pageSize,
//                          Article article) {
//        Map<String, Object> result = articleService.findPage(pageNum, pageSize, article);
//        return Result.success(result);
//    }

    /**
     * 根据ID查询文章
     */
    @GetMapping("/findById/{id}")
    public Result findById(@PathVariable Integer id) {
        Article article = articleService.findById(id);
        return Result.success(article);
    }

    /**
     * 分页条件查询文章
     */
    @GetMapping("/findArticlePage")
    public Result findArticlePage(@RequestParam(value = PAGE_NUM, defaultValue = PAGE_NUM_DEFAULT) Integer pageNum,
                                  @RequestParam(value = PAGE_SIZE, defaultValue = PAGE_SIZE_DEFAULT) Integer pageSize,
                                  ArticleQuery query) {
        Map<String, Object> result = articleService.findArticlePage(pageNum, pageSize, query);
        return Result.success(result);
    }

    /**
     * 文章回收站中的查询
     */

    @GetMapping("/findArticlePageDelelete")
    public Result findArticlePageDelelete(@RequestParam(value = PAGE_NUM, defaultValue = PAGE_NUM_DEFAULT) Integer pageNum,
                                          @RequestParam(value = PAGE_SIZE, defaultValue = PAGE_SIZE_DEFAULT) Integer pageSize,
                                          ArticleQuery query) {
        Map<String, Object> result = articleService.findArticlePageDelelete(pageNum, pageSize, query);
        return Result.success(result);
    }
    @GetMapping("/findAllDelete")
    public Result findAllDelete() {
        List<ArticleDto> articles = articleService.findAllDelete();
        return Result.success(articles);
    }
/**
 * 恢复单个文章（从回收站恢复）
 */
@PutMapping("/recover/{id}")
public Result recover(@PathVariable Integer id) {
    int result = articleService.recover(id);
    return result > 0 ? Result.success() : Result.error();
}

/**
 * 批量恢复文章（从回收站恢复）
 */
@PutMapping("/recoverBatch")
public Result recoverBatch(@RequestBody Integer[] ids) {
    int result = articleService.recoverBatch(ids);
    return result > 0 ? Result.success() : Result.error();
}

/**
 * 彻底删除单个文章
 */
@DeleteMapping("/removecompletely/{id}")
public Result removecompletely(@PathVariable Integer id) {
    int result = articleService.removecompletely(id);
    return result > 0 ? Result.success() : Result.error();
}

/**
 * 批量彻底删除文章
 */
@DeleteMapping("/removeBatchCompletely")
public Result removeBatchCompletely(@RequestBody Integer[] ids) {
    int result = articleService.removeBatchCompletely(ids);
    return result > 0 ? Result.success() : Result.error();
}

/**
 * 手动清理过期文章（超过15天）
 */
@DeleteMapping("/clearExpiredArticles")
public Result clearExpiredArticles() {
    int result = articleService.clearExpiredArticles();
    return Result.success("成功清理 " + result + " 条过期文章");
}

/**
 * 根据ID查询回收站中的文章详情
 */
@GetMapping("/findRecycleById/{id}")
public Result findRecycleById(@PathVariable Integer id) {
    ArticleDto article = articleService.findRecycleById(id);
    return Result.success(article);
}

/**
 * 测试定时清理功能（
 */
@PostMapping("/testClearExpired")
public Result testClearExpired() {
    int result = articleService.clearExpiredArticles();
    return Result.success("测试清理完成，共清理 " + result + " 条过期文章");
}

/**
 * 前端调用的回收站文章详情接口
 */
@GetMapping("/recycle/{id}")
public Result getRecycleArticle(@PathVariable Integer id) {
    ArticleDto article = articleService.findRecycleById(id);
    return Result.success(article);
}
    @PostMapping("/read/batch")
    public Result batchRead(@RequestBody List<ReadReportDto> readList) {
        for (ReadReportDto dto : readList) {
            articleService.incrementReadCount(dto.getId(),dto.getCount());
        }

        return Result.success("批量阅读量更新成功");
    }
}